package com.example.springboot159.springboot.chapter03;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements  BeanFactory{
    private static final Logger log = LoggerFactory.getLogger(AbstractBeanFactory.class);
    @Override
    public Object getBean(String name) throws Exception {
        Object obj =  getSingleton(name) ;

        if (obj != null){
            log.info("直接获取...");
            return obj;
        }

        BeanDefinition beanDefinition = getBeanDefinition(name);

        return createBean(name,beanDefinition);
    }

    @Override
    public Object getBean(String name, Object... args) throws Exception {
        Object obj =  getSingleton(name) ;

        if (obj != null){
            log.info("直接获取...");
            return obj;
        }

        BeanDefinition beanDefinition = getBeanDefinition(name);

        return createBean(name,beanDefinition,args);
    }

    //如果无法获取bean,则从自定义方法获取
    protected abstract BeanDefinition getBeanDefinition(String beanName) throws Exception;


    protected abstract Object createBean(String beanName,BeanDefinition beanDefinition) throws Exception;

    protected abstract Object createBean(String beanName,BeanDefinition beanDefinition,Object[] args) throws Exception;

}
